package com.erp.controller.supplier;

import com.erp.common.core.controller.BaseController;
import com.erp.common.core.domain.AjaxResult;
import com.erp.common.core.page.TableDataInfo;

import com.erp.domain.Supplier;
import com.erp.service.supplier.SupplierService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:lxs
 * @CreateTime: 2025-09-02
 * @Description:供应商模块
 */
@RestController
@RequestMapping("/erp/supplier")
@Slf4j
public class SupplierController extends BaseController {
    @Autowired
    private SupplierService supplierService;

    /**
     * 查询供应商列表
     */
    @RequestMapping("/list")
//    @PreAuthorize("@ss.hasPermi('erp:supplier:list')")
    public TableDataInfo list(Supplier supplier) {
        log.info("供应商列表参数：{}", supplier);
        System.out.println("beginDate: " + supplier.getBeginCreatedAt());
        System.out.println("endDate: " + supplier.getEndCreatedAt());
        // 添加参数验证
        if (supplier.getPageNum() == null || supplier.getPageNum() < 1) {
            supplier.setPageNum(1); // 默认第一页
        }
        if (supplier.getPageSize() == null || supplier.getPageSize() < 1) {
            supplier.setPageSize(10); // 默认每页10条
        }
        //分页
        PageHelper.startPage(supplier.getPageNum(),supplier.getPageSize());
        List<Supplier> list= supplierService.list(supplier);
        return getDataTable(list);
    }
    /**
     * 新增供应商
     */
    @RequestMapping("/add")
//    @PreAuthorize("@ss.hasPermi('erp:supplier:add')")
    public AjaxResult add(@RequestBody Supplier supplier){
        log.info("新增供应商参数：{}", supplier);
        return toAjax(supplierService.add(supplier));
    }
    /**
     * 修改供应商
     */
    @RequestMapping("/update")
//    @PreAuthorize("@ss.hasPermi('erp:supplier:edit')")
    public AjaxResult update(@RequestBody Supplier supplier){
        log.info("修改供应商参数：{}", supplier);
        return toAjax(supplierService.update(supplier));
    }
}
